﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;

namespace simDC
{
    public partial class archForm : Form
    {
        public Thread threadShow = null;
        Processor p = Processor.P;
        System.Drawing.Color busStandarColor = System.Drawing.Color.Gray;
        System.Drawing.Color busActiveColor = System.Drawing.Color.Tomato;
        System.Drawing.Color regStandarColor = System.Drawing.SystemColors.MenuHighlight;
        System.Drawing.Color regActiveColor = System.Drawing.Color.SandyBrown;


        
        public void WaitUpdate()
        {
            while (true)
            {
                
                Simulator.updateArchitecture.WaitOne();
               // ClearArch();
                UpdateArch();
                Simulator.updateArchitecture.Reset();
            }
        }

        public archForm()
        {
            InitializeComponent();

            microCode.View = View.Details;
            microCode.FullRowSelect = true;

            microCode.Columns.Add("Address", 100, HorizontalAlignment.Center);
            microCode.Columns.Add("Label", 100, HorizontalAlignment.Center);
            microCode.Columns.Add("SBUS", 80, HorizontalAlignment.Center);
            microCode.Columns.Add("DBUS", 80, HorizontalAlignment.Center);
            microCode.Columns.Add("ALU", 80, HorizontalAlignment.Center);
            microCode.Columns.Add("RBUS", 80, HorizontalAlignment.Center);
            microCode.Columns.Add("OtherOp", 100, HorizontalAlignment.Center);
            microCode.Columns.Add("MEM", 80, HorizontalAlignment.Center);
            microCode.Columns.Add("SUCCESOR", 200, HorizontalAlignment.Center);


            regView.View = View.Details;
            regView.FullRowSelect = true;
            regView.Columns.Add("Register", 100, HorizontalAlignment.Center);
            regView.Columns.Add("Value", 100, HorizontalAlignment.Center);

            instText.View = View.Details;
            instText.FullRowSelect = true;
            instText.Columns.Add("Address", 50, HorizontalAlignment.Center);
            instText.Columns.Add("Label", 50, HorizontalAlignment.Center);
            instText.Columns.Add("Instruction", 150, HorizontalAlignment.Left);
            
            ListViewItem li;
            string[] str = new string[2];
            for (int i = 0; i < Constants.REGISTER_NUMBER; i++)
            {
                str[0] = "R" + i.ToString();
                str[1] = p.R[i].ToString("X4");
                li = new ListViewItem(str);
                regView.Items.Add(li);
            }

            this.Show();
            CheckForIllegalCrossThreadCalls = false;

            threadShow = new Thread(new ThreadStart(WaitUpdate));
            ClearArch();
            
            threadShow.Start();
        }

        private void button1_Click(object sender, EventArgs e)
        {
           
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void label8_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            UpdateArch();
        }

        public void UpdateArch() 
        {
            ClearArch();

            PredaSBUS();
            PredaDBUS();
            PredaRBUS();
            PredaAlu();
            MemoryOP();
            OtherOperation();

        }

       
        private void OtherOperation()
        {
            if (p.OTHER_OP.Plus_2Pc)
            {
                label_RegPC.BackColor = regActiveColor;
                label_RegPC.Text = "PC = 0x" + p.PC.ToString("X4");
            }
            if (p.OTHER_OP.Plus_2Sp || p.OTHER_OP.Minus_2SP)
            {
                label_RegSP.BackColor = regActiveColor;
                label_RegSP.Text = "SP = 0x" + p.SP.ToString("X4");
            }
        }

   
        public void PredaSBUS()
        {
            regSBUS.Text = "0x" + p.SBUS.Value.ToString("X4");

            if (p.SBUS.PdSBUS)
            {
                label_PdSBUS.BackColor = busActiveColor;
                label_SBUS.BackColor = busActiveColor;
            }

            if (p.SBUS.PdIVR) label_PdIVR_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdMDR) label_PdMDR_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdFlag) label_PdFlag_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdPc) label_PdPC_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdSp) label_PdSP_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdReg) label_PdReg_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdT) label_PdT_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdIR) label_PdIR_SBUS.BackColor = busActiveColor;
            if (p.SBUS.PdADR ||p.MEM.READ || p.MEM.IFCH || p.MEM.WRITE )
            {
                label_PdADR1_SBUS.BackColor = busActiveColor;
                label_PdADR2_SBUS.BackColor = busActiveColor;
                label_PdADR3_SBUS.BackColor = busActiveColor;
            }

           
           
        }

        public void PredaDBUS() 
        {
            regDBUS.Text = "0x" + p.DBUS.Value.ToString("X4");

            if (p.DBUS.PdDBUS) 
            {
                label_PdDBUS.BackColor = busActiveColor;
                label_DBUS.BackColor = busActiveColor;            
            }

            if (p.DBUS.PdReg) label_PdReg_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdDBUS) label_PdDBUS.BackColor = busActiveColor;
            if (p.DBUS.PdFlag) label_PdFlag_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdPc) label_PdPC_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdIVR) label_PdIVR_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdSp) label_PdSP_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdT) label_PdT_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdADR) label_PdADR_DBUS.BackColor = busActiveColor;            
            if (p.DBUS.PdIR) label_PdIR_DBUS.BackColor = busActiveColor;
            if (p.DBUS.PdMDR || p.MEM.WRITE)
            {
                label_PdMDR1_DBUS.BackColor = busActiveColor;
                label_PdMDR2_DBUS.BackColor = busActiveColor;
                label_PdMDR3_DBUS.BackColor = busActiveColor;
                label_PdMDR4_DBUS.BackColor = busActiveColor;
            }

          
           
    
        }
        public void PredaAlu() 
        {
            
            lab_ALU.BackColor = regStandarColor;
            if(p.ALU.PdALU)
                label_PdAlu.BackColor = busActiveColor;
            
            if (p.ALU.Flag)
            {
                label_PdCond3.BackColor = busActiveColor;
                label_PdCond2.BackColor = busActiveColor;
                label_PdCond1.BackColor = busActiveColor;
                label_PmFlag.BackColor = busActiveColor; 
            }

            lab_ALU.Text = "NONE";
            if (p.ALU.SBUS) { lab_ALU.Text = "SBUS"; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.Add) { lab_ALU.Text = "ADD"; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.And) { lab_ALU.Text = "AND"; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.NSBUS) { lab_ALU.Text = "!SBUS"; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.Or) { lab_ALU.Text = "OR" ; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.Xor) { lab_ALU.Text = "XOR"; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.DBUS) { lab_ALU.Text = "DBUS"; lab_ALU.BackColor = regActiveColor; }
            if (p.ALU.Mul) { lab_ALU.Text = "MUL"; lab_ALU.BackColor = regActiveColor; }
            

            regRBUS.Text = "0x" + p.RBUS.Value;
        }

        public void PredaRBUS()
        {
           // regRBUS.Text = "0x"+p.RBUS.Value.ToString("X4");
            //initializare registre 
            label_RegIVR.BackColor = regStandarColor;
            label_RegADR.BackColor = regStandarColor;
            label_RegSP.BackColor = regStandarColor;
            label_RegPC.BackColor = regStandarColor;
            label_RegT.BackColor = regStandarColor;
            label_RegFLAG.BackColor = regStandarColor;
            label_RegMDR.BackColor = regStandarColor;

            if (p.ALU.PdALU)
            {
                label_RBUS1.BackColor = busActiveColor;
               // label_RBUS2.BackColor = busActiveColor;
            }

            if (p.RBUS.PmIVR)
            {
                label_PmIVR.BackColor = busActiveColor;
                label_RegIVR.BackColor = regActiveColor;
                label_RegIVR.Text = "IVR=0x" + p.IVR.ToString("X4");
            }
            if (p.RBUS.PmADR)
            {
                label_PmADR.BackColor = busActiveColor;
                label_RegADR.Text = "ADR=0x" + p.ADR.ToString("X4");
                label_RegADR.BackColor = regActiveColor;
            }
            if (p.RBUS.PmSp)
            {
                label_PmSp.BackColor = busActiveColor;
                label_RegSP.Text = "SP=0x" + p.SP.ToString("X4");
                label_RegSP.BackColor = regActiveColor;
            }
            if (p.RBUS.PmPC)
            {
                label_PmPc.BackColor = busActiveColor; 
                label_RegPC.Text = "PC=0x" + p.PC.ToString("X4");
                label_RegPC.BackColor = regActiveColor;
            }
            if (p.RBUS.PmReg)
                label_PmReg.BackColor = busActiveColor;
            {
                regView.Items.Clear();
               
                ListViewItem li;
                string[] str = new string[2];
                for (int i = 0; i < Constants.REGISTER_NUMBER; i++) 
                {
                    str[0] = "R" + i.ToString();
                    str[1] = p.R[i].ToString("X4");
                    li = new ListViewItem(str);
                    regView.Items.Add(li);
                }
                             
            }
            if (p.RBUS.PmT)
            {
                label_PmT.BackColor = busActiveColor;
                label_RegT.Text = "T=0x" + p.T.ToString("X4");
                label_RegT.BackColor = regActiveColor;
            }
            if (p.RBUS.PmFlag)
                {
                    label_PmFlag_RBUS.BackColor = busActiveColor; 
                    label_PmFlag.BackColor = busActiveColor;
                    label_RegFLAG.Text = "FLAG=0x" + p.FLAG.ToString("X2");
                    label_RegFLAG.BackColor = regActiveColor;
                }
                if (p.RBUS.PmMDR)
                {
                    label_PmMDR1.BackColor = busActiveColor; 
                    label_PmMDR2.BackColor = busActiveColor;
                    label_PmMDR3.BackColor = busActiveColor;
                    label_RegMDR.Text = "MDR=0x" + p.MDR.ToString("X4");
                    label_RegMDR.BackColor = regActiveColor;
                }
                if (p.OTHER_OP.Flags | p.ALU.Flag | p.RBUS.PmFlag)
                {
                    label_RegFLAG.Text = "FLAG=0x" + p.FLAG.ToString("X2");
                    label_RegFLAG.BackColor = regActiveColor;
                }


                          
              //  lab_ALU.BackColor = regStandarColor;
               // label_RegIR.Text = "IR=0x" + p.IR.ToString("X4");
              
        }
        public void MemoryOP()
        {
            label_RegIR.BackColor = regStandarColor;
            
            if (p.MEM.IFCH) { label_RegIR.Text = "IR=0x" + p.IR.ToString("X4"); label_RegIR.BackColor = regActiveColor; }
            if (p.MEM.READ)
            {
                label_PmMDR1.BackColor = busActiveColor;
                label_PmMDR2.BackColor = busActiveColor;
                label_RegMDR.Text = "MDR=0x" + p.MDR.ToString("X4");
                label_RegMDR.BackColor = regActiveColor;
            }
            if (p.MEM.IFCH | p.MEM.READ) 
            {
                label_MemRead1.BackColor = busActiveColor;
                label_MemRead2.BackColor = busActiveColor;
                label_MemRead3.BackColor = busActiveColor;
                label_MemRead4.BackColor = busActiveColor;
                label_MemRead5.BackColor = busActiveColor;
            }
            if (p.MEM.WRITE) 
            {
                label_PdMDR1_DBUS.BackColor = busActiveColor;
                label_PdMDR2_DBUS.BackColor = busActiveColor;
                label_PdMDR3_DBUS.BackColor = busActiveColor;
                label_PdMDR4_DBUS.BackColor = busActiveColor;
                label_PdADR1_SBUS.BackColor = busActiveColor;
                label_PdADR2_SBUS.BackColor = busActiveColor;
                label_PdADR3_SBUS.BackColor = busActiveColor;
            }


        }

        public void ClearArch() 
        {

            label_PmIVR.BackColor = busStandarColor;
            label_PmADR.BackColor = busStandarColor;
            label_PmSp.BackColor = busStandarColor;
            label_PmPc.BackColor = busStandarColor;            
            label_PmReg.BackColor = busStandarColor;
            label_PmT.BackColor = busStandarColor;
            label_PmFlag_RBUS.BackColor = busStandarColor;
            label_PmFlag.BackColor = busStandarColor;
            label_PmMDR1.BackColor = busStandarColor;
            label_PmMDR2.BackColor = busStandarColor;
            label_PmMDR3.BackColor = busStandarColor;

            label_PdIVR_SBUS.BackColor = busStandarColor;
            label_PdMDR_SBUS.BackColor = busStandarColor;           
            label_PdFlag_SBUS.BackColor = busStandarColor;
            label_PdSBUS.BackColor = busStandarColor;
            label_PdPC_SBUS.BackColor = busStandarColor;
            label_PdSP_SBUS.BackColor = busStandarColor;
            label_PdReg_SBUS.BackColor = busStandarColor;
            label_PdT_SBUS.BackColor = busStandarColor;
            label_PdIR_SBUS.BackColor = busStandarColor;

            label_PdAlu.BackColor = busStandarColor;
            
            label_PdReg_DBUS.BackColor = busStandarColor;
            label_PdDBUS.BackColor = busStandarColor;           
            label_PdFlag_DBUS.BackColor = busStandarColor;           
            label_PdPC_DBUS.BackColor = busStandarColor;
            label_PdIVR_DBUS.BackColor = busStandarColor;
            label_PdSP_DBUS.BackColor = busStandarColor;
            label_PdT_DBUS.BackColor = busStandarColor;
            label_PdADR_DBUS.BackColor = busStandarColor;           
            label_PdIR_DBUS.BackColor = busStandarColor;

            label_PdADR1_SBUS.BackColor = busStandarColor;
            label_PdADR2_SBUS.BackColor = busStandarColor;
            label_PdADR3_SBUS.BackColor = busStandarColor;
            
            label_PdCond3.BackColor = busStandarColor;
            label_PdCond2.BackColor = busStandarColor;
            label_PdCond1.BackColor = busStandarColor;

            label_PdMDR1_DBUS.BackColor = busStandarColor;
            label_PdMDR2_DBUS.BackColor = busStandarColor;
            label_PdMDR3_DBUS.BackColor = busStandarColor;           
            label_PdMDR4_DBUS.BackColor = busStandarColor;
           
            label_MemRead1.BackColor = busStandarColor;
            label_MemRead2.BackColor = busStandarColor;
            label_MemRead3.BackColor = busStandarColor;
            label_MemRead4.BackColor = busStandarColor;
            label_MemRead5.BackColor = busStandarColor;

            label_DBUS.BackColor = busStandarColor;
            label_SBUS.BackColor = busStandarColor;
            label_RBUS1.BackColor = busStandarColor;          
            
            
            
            label_RegMDR.BackColor = regStandarColor; 
            lab_ALU.BackColor = regStandarColor;
            label_RegIR.BackColor = regStandarColor;
            label_RegADR.BackColor = regStandarColor;
            label_RegPC.BackColor = regStandarColor;
            label_RegIVR.BackColor = regStandarColor;
            label_RegFLAG.BackColor = regStandarColor;
            label_RegT.BackColor = regStandarColor;
            label_RegSP.BackColor = regStandarColor;

            label_RegMDR.Text = "MDR=0x" + p.MDR.ToString("X4");
            lab_ALU.BackColor = regStandarColor;
            label_RegIR.Text = "IR=0x" + p.IR.ToString("X4");
            label_RegADR.Text = "ADR=0x" + p.ADR.ToString("X4");
            label_RegPC.Text = "PC=0x" + p.PC.ToString("X4");
            label_RegIVR.Text = "IVR=0x" + p.IVR.ToString("X4");
            label_RegFLAG.Text = "FLAG=0x" + p.FLAG.ToString("X2");
            label_RegT.Text = "T=0x" + p.T.ToString("X4");
            label_RegSP.Text = "SP=0x" + p.SP.ToString("X4");



            lab_ALU.BackColor = regStandarColor;
            lab_ALU.Text = "NONE";


          
        }

        private void archForm_FormClosed(object sender, FormClosedEventArgs e)
        {

        }

        private void archForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;    
        }

        private void archForm_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            UpdateArch();
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }

       

       
        
    }
}
